package com.whq.knowverywell.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.whq.knowverywell.R;
import com.whq.knowverywell.business.model.RegionSalesRecord;
import com.whq.knowverywell.business.model.StoreSalesRecord;
import com.whq.knowverywell.util.LogUtil;
import com.whq.knowverywell.util.constant.ModelConstant;

/**
 * content list adapter
 * 
 * @author daiwei
 * 
 */
public class ContentListAdapter extends BaseAdapter {
	private static final String TAG = ContentListAdapter.class.getSimpleName();

	private LayoutInflater inflater;
	private Activity ctx;
	private List resultList;
	private int type;

	public ContentListAdapter(Activity ctx, List resultList, int type) {
		this.type = type;
		this.resultList = resultList;
		this.ctx = ctx;

		try {
			this.inflater = LayoutInflater.from(ctx);
		} catch (Exception e) {
			LogUtil.e(TAG, "exception:", e);
		}
		if (this.resultList == null) {
			this.resultList = new ArrayList();
		}
	}

	@Override
	public int getCount() {
		return resultList.size();
	}

	@Override
	public Object getItem(int position) {
		return resultList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SummaryViewHolder huizongViewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.huizong_item, null);
			huizongViewHolder = new SummaryViewHolder();
			huizongViewHolder.mallTextView = (TextView) convertView
					.findViewById(R.id.mall_textview);
			huizongViewHolder.sellAmountTextView = (TextView) convertView
					.findViewById(R.id.sell_amount_textview);
			huizongViewHolder.sellCountTextView = (TextView) convertView
					.findViewById(R.id.sell_count_textview);
			huizongViewHolder.synchronousSellAmountTextView = (TextView) convertView
					.findViewById(R.id.synchronous_sell_amount_textview);
			huizongViewHolder.synchronousSellCountTextView = (TextView) convertView
					.findViewById(R.id.synchronous_sell_count_textview);

			convertView.setTag(huizongViewHolder);
		} else {
			huizongViewHolder = (SummaryViewHolder) convertView.getTag();
		}

		switch (type) {
		case ModelConstant.BUSINESS_TYPE_SUMMARY:
			StoreSalesRecord record = (StoreSalesRecord) getItem(position);
			if (record == null) {
				LogUtil.e(TAG, "the position[" + position + "] item is null .");
				return convertView;
			}

			huizongViewHolder.mallTextView.setText(record.getName());
			huizongViewHolder.sellAmountTextView.setText("￥ "
					+ record.getSalesAmount() + "");
			huizongViewHolder.sellCountTextView.setText(record.getSalesVolume()
					+ "");
			huizongViewHolder.synchronousSellAmountTextView.setText(record
					.getLastTermSalesAmount() + "");
			huizongViewHolder.synchronousSellCountTextView.setText(record
					.getLastTermSalesVolume() + "");
			break;
			
		case ModelConstant.BUSINESS_TYPE_DOMAIN:
			RegionSalesRecord domainRecord = (RegionSalesRecord) getItem(position);
			if (domainRecord == null) {
				LogUtil.e(TAG, "the position[" + position + "] item is null .");
				return convertView;
			}

			huizongViewHolder.mallTextView.setText(domainRecord.getName());
			huizongViewHolder.sellAmountTextView.setText("￥ "
					+ domainRecord.getSalesAmount() + "");
			huizongViewHolder.sellCountTextView.setText(domainRecord.getSalesVolume()
					+ "");
			break;

		default:
			break;
		}
		

		return convertView;
	}

	class SummaryViewHolder {
		TextView mallTextView, sellAmountTextView,
				synchronousSellAmountTextView, sellCountTextView,
				synchronousSellCountTextView;
	}

}
